/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package com.fzhucc.modules.h5Area.controller;

import com.fzhucc.annotation.AnonymousAccess;
import com.fzhucc.annotation.Log;
import com.fzhucc.modules.h5Area.domain.H5Area;
import com.fzhucc.modules.h5Area.service.H5AreaService;
import com.fzhucc.modules.h5Area.domain.dto.H5AreaQueryCriteria;
import com.fzhucc.modules.sciCxResult.domain.dto.SciCxResultQueryCriteria;
import org.springframework.data.domain.Pageable;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

/**
 * @author yyg
 * @date 2024-02-07
 */
@RestController
@RequiredArgsConstructor
@Api(tags = "h5地区管理管理")
@RequestMapping("/api/h5Area")
public class H5AreaController {

    private final H5AreaService h5AreaService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('h5Area:list')")
    public void exportH5Area(HttpServletResponse response, H5AreaQueryCriteria criteria) throws IOException {
        h5AreaService.download(h5AreaService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询h5地区管理")
    @ApiOperation("查询h5地区管理")
    @PreAuthorize("@el.check('h5Area:list')")
    public ResponseEntity<Object> queryH5Area(H5AreaQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(h5AreaService.queryAll(criteria,pageable), HttpStatus.OK);
    }

    @AnonymousAccess
    @GetMapping(value = "/list")
    @Log("h5、小程序查询h5地区管理")
    @ApiOperation("h5、小程序h5地区管理")
    public ResponseEntity<Object> list(H5AreaQueryCriteria criteria, Pageable pageable) {
        criteria.setIsShow(1);
        return new ResponseEntity<>(h5AreaService.queryAllh5(criteria, pageable), HttpStatus.OK);
    }

    @PostMapping
    @Log("新增h5地区管理")
    @ApiOperation("新增h5地区管理")
    @PreAuthorize("@el.check('h5Area:add')")
    public ResponseEntity<Object> createH5Area(@Validated @RequestBody H5Area resources){
        return new ResponseEntity<>(h5AreaService.create(resources), HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改h5地区管理")
    @ApiOperation("修改h5地区管理")
    @PreAuthorize("@el.check('h5Area:edit')")
    public ResponseEntity<Object> updateH5Area(@Validated @RequestBody H5Area resources){
        h5AreaService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除h5地区管理")
    @ApiOperation("删除h5地区管理")
    @PreAuthorize("@el.check('h5Area:del')")
    public ResponseEntity<Object> deleteH5Area(@RequestBody Integer[] ids) {
        h5AreaService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}